import os
import time
from datetime import datetime

import settings
# 处理每个微信的自动运行时的 业务功能，，配置，

# 判断是否有配置文件，如果没有则写入，

from weixin_modules.inner_settings import users_config_dir
from conf_json_rw import json_write, json_read, text_read, text_write
from settings import Function_Template


def get_task_message(wxid):
    task_text = "task.txt"
    task_dir = os.path.join(users_config_dir, wxid)
    if not os.path.exists(task_dir):
        os.makedirs(task_dir)
    task_path = os.path.join(task_dir, task_text)
    if os.path.exists(task_path):
        stat = os.stat(task_path)
        if stat.st_size > 0:
            message = text_read(task_path)
            return message
    message = settings.task_message
    text_write(task_path, message)
    return message


def get_funs(wxid):
    functions_dir = os.path.join(users_config_dir, wxid)
    if not os.path.exists(functions_dir):
        os.makedirs(functions_dir)

    funs_file = "funs.json"
    funs_file_path = os.path.join(functions_dir, funs_file)
    if os.path.exists(funs_file_path):
        stat = os.stat(funs_file_path)
        if stat.st_size > 0:  # 如果文件大小大于0，那么文件存在
            # print("文件存在")
            funs_dict = json_read(funs_file_path)
            return funs_dict
    # json配置文件不存在,则按默认的配置创建,
    json_write(funs_file_path, Function_Template)
    return Function_Template


def is_time_ready(target_hour, target_minute=0):
    """
    定时执行任务，
    :param target_hour:
    :param target_minute:
    :return:
    """
    now = datetime.now()
    if now.hour == target_hour and now.minute > target_minute:
        return True
    return False

def is_time_run(target_hour):
    """
    定时执行任务，
    :param target_hour:
    :param target_minute:
    :return:
    """
    now = datetime.now()
    if now.hour <= target_hour:
        return True
    return False

if __name__ == '__main__':
    wxid = "test43234"
    print(get_funs(wxid))
